<!--
 * @Author: wuzhen
 * @Date: 2020-01-13 13:33:46
 * @LastEditors  : wuzhen
 * @LastEditTime : 2020-01-19 09:19:52
 * @Description: 扫码登录
 -->
<template>
  <div class="page">
    <div class="page-code">
      <div class="logo">
        <img :src="logoImg" />
        <span>{{$t("web_title")}}</span>
      </div>
      <div class="qrcode">
        <!-- 二维码加载中 -->
        <wz-load @on-reload="onReload" ref="wzLoad" v-if="isLoading"/>
        <!-- 二维码加载成功 -->
        <template v-if="!isLoading">
          <div class="qrcode-img">
            <img :src="qrcodeImg"/>
            <!-- 二维码过期 -->
            <div class="icon-refresh" v-if="!isValid || isLogining">
              <wz-load v-if="isLogining"/>
              <img :src="refreshImg" :class="{isRefresh:isRefresh}" @click="onRefresh" v-else/>
            </div>
          </div>
          <template v-if="isValid">
            <p class="sub_title">{{$t("web_tip1")}}</p>
            <p class="sub_desc">{{$t("web_tip2")}}</p>
          </template>
          <p class="sub_title" v-else-if="isLogining">正在登录中...</p>
          <!-- 二维码过期 -->
          <p class="tips" v-else>{{$t("web_tip3")}}</p>
        </template>
      </div>
    </div>
    <div class="page-footer flex">
      <p class="flex-item">
        <span @click="changeLanguage('zh_CN')">{{$t("zh_CN")}}</span>|
        <span @click="changeLanguage('zh_TW')">{{$t("zh_TW")}}</span>|
        <span @click="changeLanguage('en_US')">{{$t("en_US")}}</span>
      </p>
      <p>© 2019-2020 Butter. All Rights Reserved</p>
    </div>
  </div>
</template>
<script>
import { getLoginQrcode, checkScanStatus, checkWebLogin } from '@/request/api';
import { imgUrl } from "@/config/";
import { Message, Spin } from "view-design";
import { mapMutations } from "vuex";
export default {
  components: {
    Message, Spin
  },
  inject: ["reload"],
  data() {
    return {
      qrcodeImg: null,
      logoImg: require("@/assets/image/logo.png"),
      refreshImg: require("@/assets/image/icon_refresh.png"),
      isLoading: false, // 二维码加载中
      isValid: true, // 是否有效
      isLogining: false, // 二维码加载中
      isRefresh: false, // 正在刷新二维码
      timerCount: 0
    };
  },
  created() {
    this.isLoading = true;
    this.getQrcode();
  },
  methods: {
    ...mapMutations(["set_local"]),
    // 获取二维码
    async getQrcode() {
      try {
        this.timerCount = 0;
        const res = await getLoginQrcode();
        if (res.code === 200) {
          this.uuidKey = res.result.uuidKey;
          this.qrcodeImg = `${imgUrl}/qrcode/${res.result.filename}`;
          this.checkStatus();
          setTimeout(() => {
            this.isLoading = false;
          }, 1000);
        } else {
          this.$refs["wzLoad"].isReloading();
        }
      } catch (error) {
        this.$refs["wzLoad"].isReloading();
        throw error;
      }
    },
    // 检查登录状态
    async checkStatus() {
      try {
        const res = await checkScanStatus({uuidKey: this.uuidKey});
        this.isRefresh = false;
        this.isValid = true;
        // 二维码失效
        if (res.retcode === 30080001) {
          clearTimeout(this.statusTimer);
          this.isValid = false;
          return;
        }
        switch (res.result) {
          case '0':
            this.statusTimer = setTimeout(() => {
              this.checkStatus();
            }, 3000);
            break;
          case '1':
            clearTimeout(this.statusTimer);
            this.statusTimer = setTimeout(() => {
              this.checkStatus();
            }, 3000);
            break;
          case '2':
            this.checkQrcode();
            break;
          case '3':
            clearTimeout(this.statusTimer);
            this.getQrcode();
            break;
          default:
            break;
        }
      } catch (error) {
        this.checkStatus();
        throw error;
      }
    },
    // 二维码扫码回调
    async checkQrcode() {
      this.isValid = false;
      this.isLogining = true;
      try {
        const res = await checkWebLogin({uuidKey: this.uuidKey});
        if (res.retcode === 0 && res.result.split('#')[0] !=='null') {
          const jsessionId = res.result.split('#')[0];
          const unf = res.result.split('#')[1];
          this.set_jsessionId(jsessionId);
          this.set_unf(unf);
          this.$router.push("chat");
        } else {
          Message.error('登录异常，请重新扫码登录！');
          this.getQrcode();
        }
        this.isValid = true;
        this.isLogining = false;
      } catch (error) {
        this.isValid = true;
        this.isLogining = false;
        throw error;
      }
    },
    // 重新加载
    onReload() {
      this.isLoading = true;
      this.getQrcode();
    },
    // 刷新
    onRefresh() {
      this.isRefresh = true;
      this.getQrcode();
    },
    // 语言
    changeLanguage(local) {
      this.$i18n.locale = local;
      this.set_local(local);
      this.reload();
    }
  }
};
</script>
<style lang="less" scoped>
.page {
  position: relative;
  min-height: 800px;
  height: 100vh;
  &-code {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -55%);
    .logo {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 45px;
      img {
        width: 54px;
        height: 54px;
        margin-right: 15px;
      }
      span {
        font-size: 32px;
        color: #fff;
      }
    }
    .qrcode {
      text-align: center;
      font-size: 20px;
      color: #343434;
      line-height: 36px;
      background: #fff;
      box-shadow: 0px 5px 10px 0px rgba(144, 144, 144, 0.5);
      border-radius: 5px;
      padding: 30px 20px;
      position: relative;
      height: 450px;
      width: 340px;
      .tips {
        color: #fa5950;
      }
      &-img{
        position: relative;
        margin-bottom: 20px;
        width: 300px;
        height: 300px;
        .icon-refresh {
          background: rgba(255, 255, 255, 0.9);
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          bottom: 0;
          z-index: 111;
          display: flex;
          align-items: center;
          justify-content: center;
          img {
            width: 118px;
            height: 118px;
            &.isRefresh {
              animation: rote 0.8s linear infinite;
            }
          }
        }
        img {
          display: block;
          width: 300px;
          height: 300px;
        }
      }
      &-load{
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        background: rgba(255, 255, 255, 0.9);
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
  &-footer{
    position: absolute;
    bottom: 30px;
    left: 10%;
    right: 10%;
    p{
      color: #fff;
      span{
        margin: 0 10px;
        cursor: pointer;
      }
    }
  }
}
@keyframes rote {
  0% {
    transform: rotate(0deg);
  }

  25% {
    transform: rotate(90deg);
  }

  50% {
    transform: rotate(180deg);
  }

  75% {
    transform: rotate(270deg);
  }

  100% {
    -webkit-transform: rotate(360deg);
  }
}
</style>
